// Copyright 2022 a.o.smith ALL Rights Reserved
package com.liuxinlong.modules.patent.service.impl;

import com.liuxinlong.common.AosException;
import com.liuxinlong.common.SnowFlake;
import com.liuxinlong.constant.ThreadLocalConstants;
import com.liuxinlong.enums.ReturnCodes;
import com.liuxinlong.modules.dao.PatentNoticeDao;
import com.liuxinlong.modules.entity.PatentNotice;
import com.liuxinlong.modules.entity.User;
import com.liuxinlong.modules.patent.service.PatentNoticeService;
import com.liuxinlong.utils.ObjectUtils;
import com.liuxinlong.utils.ThreadlocalUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 专利公告实现类
 *
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2023-09-15
 */
@Service
@Slf4j
public class PatentNoticeServiceImpl implements PatentNoticeService {

    @Autowired
    private PatentNoticeDao patentNoticeDao;

    @Override
    public List<Map<String, Object>> queryPatentNoticeList(Map<String, Object> queryParam) {
        List<PatentNotice> patentNoticeList = patentNoticeDao.pagePatentNoticeList(queryParam);
        List<Map<String,Object>> resultList = completeResult(patentNoticeList,(int) queryParam.get("startNum"));
        return resultList;
    }

    @Override
    public int getPatentNoticeCount(Map<String, Object> queryParam) {
        return patentNoticeDao.getPatentNoticeCount(queryParam);
    }

    @Override
    public void addPatentNotice(PatentNotice patentNotice) {
        patentNotice.setId(SnowFlake.nextIdStr());
        patentNotice.setStatus(2);
        User currentUser = (User) ThreadlocalUtils.getCache(ThreadLocalConstants.USER_INFO);
        patentNotice.setCreateUser(currentUser.getId());
        patentNoticeDao.insert(patentNotice);
    }

    @Override
    public void updatePatentNotice(PatentNotice patentNotice) {
        PatentNotice oldInfo = patentNoticeDao.selectById(patentNotice.getId());
        if (ObjectUtils.isEmpty(oldInfo)) {
            throw new AosException(ReturnCodes.HTTP_PARAM_ERROR,"修改失败，公告信息不存在！");
        }
        patentNotice.setStatus(oldInfo.getStatus());
        patentNotice.setCreateUser(oldInfo.getCreateUser());
        patentNotice.setCreateTime(oldInfo.getCreateTime());
        User currentUser = (User) ThreadlocalUtils.getCache(ThreadLocalConstants.USER_INFO);
        patentNotice.setUpdateUser(currentUser.getId());
        patentNoticeDao.updateById(patentNotice);
    }

    @Override
    public void deletePatentNotice(String id) {
        PatentNotice oldInfo = patentNoticeDao.selectById(id);
        if (ObjectUtils.isEmpty(oldInfo)) {
            throw new AosException(ReturnCodes.HTTP_PARAM_ERROR,"删除失败，公告信息不存在！");
        }
        patentNoticeDao.deleteById(id);
    }

    /**
     * 补全数据
     *
     * @param originalList 原始数据
     * @param startNum 起始数据
     * @return 补全后数据
     */
    private List<Map<String,Object>> completeResult( List<PatentNotice> originalList ,int startNum){
        List<Map<String,Object>> resultList = new ArrayList<>(originalList.size());
//        Map<String,Map<String,String>> departmentMap = patentDepartmentDao.getDepartmentMap();
        int sort = startNum + 1;
        for (PatentNotice item : originalList){
            Map<String,Object> map = ObjectUtils.object2Map(item);
            map.put("sort",sort);
//            map.put("departmentName",departmentMap.containsKey(item.getDepartment()) ? departmentMap.get(item.getDepartment()).get("name") : "");
            resultList.add(map);
            sort ++;
        }
        return resultList;
    }

}
